// page/waypoint/waypoint.js
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');

Page({
    data: {
        options: null,
		viewWaypoint: null,
		replies: null
    },
    onLoad: function(options) {
        let self = this;
		let wayId = options.waypointId;
		let tripId = options.tripId;
        self.setData({
            options
        });
		wx.showToast({
			title: '正在加载',
			icon: 'loading',
			duration: 10000,
			mask: true
		});
		api.viewWaypoint.detail(tripId,wayId,(state,res) => {
			if(state === 'success'){
				// console.log(res.data);
				let viewWaypoint = res.data;
				self.setData({
					viewWaypoint
				})
				if (viewWaypoint.comments > 0){
					self.getReplies(tripId, wayId)
				}
				wx.hideToast();
			}
		})
    },
	getReplies(tripId, wayId){
		let self = this;
		api.viewWaypoint.replies(tripId, wayId, (state, res) => {
			if (state === 'success') {
				// console.log(res.data);
				let replies = res.data;
				replies.comments.map((comment) => {
					let item = comment;
					item.date_added = util.formatTime(new Date(item.date_added*1000),2);
					return item;
				})
				self.setData({
					replies
				})
			}
		})
	},
	goUser: function (e) {
		let dataUser = e.currentTarget.dataset;
		wx.navigateTo({
			url: `../user/user?id=${dataUser.id}`
		})
	},
	previewImg:function(e){
		let dataImg = e.currentTarget.dataset.src;
		wx.previewImage({
			urls: [dataImg]
		})
	}
})